package com.finance.system.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.finance.system.support.bean.OffsetQuery;
import com.finance.system.support.bean.OffsetResult;
import com.finance.system.support.bean.ResultBean;
import com.finance.system.support.bean.ResultConstant;
import com.finance.system.support.utils.TimeFormatUtil;
import com.finance.system.user.entity.*;
import com.finance.system.user.enums.GroupType;
import com.finance.system.user.service.IGroupService;
import com.finance.system.user.service.IUserGroupRelationService;
import com.finance.system.user.mapper.UserMapper;
import com.finance.system.user.service.IUserService;
import com.finance.system.support.BaseServiceImpl;
import com.finance.system.support.utils.DomainTransHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表，存放系统用户信息 服务实现类
 * </p>
 *
 * @author whdong
 * @since 2019-12-24
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserEntity> implements IUserService {

    @Autowired
    private IUserGroupRelationService userGroupRelationService;
    @Autowired
    private IGroupService groupService;

    @Override
    public OffsetResult<UserOutbound> getUserList(UserInbound user, OffsetQuery<UserEntity> offsetQuery) {
        QueryWrapper example = getQueryWrapperByCondition(user);
        IPage<UserEntity> page = baseMapper.selectPage(offsetQuery, example);
        return OffsetResult.build(page,UserOutbound.class);
    }

    @Override
    public Map<String, UserEntity> getUserMapByIds(Set<Long> ids) {
        return null;
    }

    @Override
    public UserEntity getByUserName(String username) {
        QueryWrapper queryWrapper = new QueryWrapper<UserEntity>();
        queryWrapper.eq("user_name",username);

        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public UserEntity getByPhone(String phone) {
        QueryWrapper queryWrapper = new QueryWrapper<UserEntity>();
        queryWrapper.eq("phone",phone);

        return baseMapper.selectOne(queryWrapper);
    }

    private QueryWrapper getQueryWrapperByCondition(UserInbound user) {
        QueryWrapper queryWrapper = new QueryWrapper<UserEntity>();
        if(user.getStartTime() != null){
            queryWrapper.gt("consume_date",
                    TimeFormatUtil.formatDateYYYYMMDDHHMMSS(user.getStartTime()));
        }
        if(user.getEndTime() != null){
            queryWrapper.lt("consume_date",
                    TimeFormatUtil.formatDateYYYYMMDDHHMMSS(user.getEndTime()));
        }

        return queryWrapper;
    }


    @Override
    public ResultBean<List<UserEntity>> getUserByGroupId(String groupId) {
        if(StringUtils.isEmpty(groupId)){
            return ResultBean.create(ResultConstant.SUCCESS_CODE,ResultConstant.SUCCESS_MESSAGE, Collections.emptyList());
        }
        List<UserGroupRelationEntity> relations = userGroupRelationService.getByGroupId(groupId,null);
        if(CollectionUtils.isEmpty(relations)){
            return ResultBean.create(ResultConstant.SUCCESS_CODE,ResultConstant.SUCCESS_MESSAGE, Collections.emptyList());
        }
        Collection<UserEntity> users =
                this.listByIds(relations.stream().map(UserGroupRelationEntity::getUserId).collect(Collectors.toList()));

        return ResultBean.create(ResultConstant.SUCCESS_CODE,ResultConstant.SUCCESS_MESSAGE,
                new ArrayList<>(users));
    }



    @Override
    public ResultBean addUserInToGroup(UserGroupRelationEntity relation) {
        if(StringUtils.isEmpty(relation.getGroupId())){
            return ResultBean.create(ResultConstant.ERROR_CODE,ResultConstant.ERROR_MESSAGE_NO_GROUP_ID,null);
        }
        if(StringUtils.isEmpty(relation.getUserId())){
            return ResultBean.create(ResultConstant.ERROR_CODE,ResultConstant.ERROR_MESSAGE_NO_USER_ID,null);
        }
        userGroupRelationService.save(relation);
        return ResultBean.create(ResultConstant.SUCCESS_CODE,ResultConstant.ADD_SUCCESS_MESSAGE,null);
    }


}
